<template>
    <!-- 登录界面 -->
    <div class="login_card">
        <div class="login_card--title">
            <div>Hello<span>oooo</span></div>
        </div>
        <div class="login_card--input">
            <div>
                <input
                    type="text"
                    v-model="user.account"
                    placeholder="Your Account"
                />
                <hr />
                <input
                    type="password"
                    v-model="user.password"
                    placeholder="Your Password"
                />
                <hr />
                <input
                    type="password"
                    v-model="user.email"
                    placeholder="Your E-mail"
                    :class="
                        login_card.state === 1
                            ? 'login_card--input_register_t'
                            : 'login_card--input_register_f'
                    "
                />
                <hr
                    :class="
                        login_card.state === 1
                            ? 'login_card--input_register_hr_t'
                            : 'login_card--input_register_hr_f'
                    "
                />
            </div>
        </div>
        <div class="login_card--checkbox">
            <!-- <input type="checkbox" placeholder="Your E-mail" /> -->
            <div @click="changeCheckbox" :class="checkbox_CSS">
                <span>Remember me</span>
            </div>
        </div>
        <div class="login_card--holder"></div>
        <div class="login_card--button">
            <div class="login_card--button_a" @click="sendMessage(0)">
                Login
            </div>
            <div class="login_card--button_b" @click="sendMessage(1)">
                {{ login_card.value }}
            </div>
            <div class="login_card--button_c" @click="sendMessage(2)">Back</div>
        </div>
    </div>
</template>

<script setup>
import { ref, reactive, inject } from "vue";
const axios = inject("axios");

// 事件
const emits = defineEmits(["emitValue"]);
// 事件状态
const emitValue = reactive({
    value: null,
    msg: null,
});

// 用户状态
const user = reactive({
    account: null,
    password: null,
    remember: false,
    email: null,
});
// 组件状态
const login_card = reactive({
    value: "Register",
    state: 0,
    login: false,
});

// 向父组件发送信息
const sendMessage = (index) => {
    switch (index) {
        case 0: {
            // 验证表单

            if (login_card.value === "Register") {
                if (user.account && user.password) {
                    Login(() => {
                        if (login_card.login) {
                            // 发送的值
                            emitValue.value = 0;
                            emitValue.msg = {
                                user_account: user.account,
                                user_password: user.password,
                                user_remember: user.remember,
                            };
                            emits("emitValue", emitValue);
                        }
                    });
                } else {
                    console.log("请输入");
                }
            } else {
                // 回到默认样式
                checkbox_CSS.value = "login_card--checkbox_f";
                user.remember = false;
            }

            // 设置值
            login_card.value = "Register";
            login_card.state = 0;

            break;
        }
        case 1: {
            // 验证表单
            if (login_card.value === "Segin") {
                if (user.account && user.password && user.email) {

                    console.log("缺少注册接口");

                    // 发送的值
                    emitValue.value = 1;
                    emitValue.msg = {
                        user_account: user.account,
                        user_password: user.password,
                        user_email: user.email,
                    };
                    emits("emitValue", emitValue);
                } else {
                    console.log("请输入");
                }
            }

            // 设置值
            login_card.value = "Segin";
            checkbox_CSS.value = "login_card--checkbox_uninstall";
            login_card.state = 1;

            break;
        }
        case 2: {
            // 发送的值
            emitValue.value = 2;
            emits("emitValue", emitValue);
            break;
        }
    }
};

// 登录
const store_data = inject("storeEP").state;
const storeEP = inject("storeEP");
function Login(callback) {
    let res = axios.post("/login/", {
        user_account: user.account,
        user_password: user.password,
    }).then((res) => {
        if (!res.headers.authorization) {
            console.log("登录失败");
            if (res.data.code === 401) {
                console.log("请注册");
                // 跳转到注册页面、注册路由
                return;
            }
            console.log("未知错误");
            return;
        } else {
            storeEP.state.token = res.headers.authorization;
            storeEP.state.user_account = user.account;

            console.log(res.headers.authorization);

            if (user.remember) {
                // 本地储存
                storeEP.setStore("USER_ACCOUNT", storeEP.state.user_account);
                storeEP.setStore("TOKEN", storeEP.state.token);
            } else {
                // storeEP.setStore("USER_ACCOUNT", storeEP.state.user_account);
                console.log("未本地储存");
            }

            console.log("登录成功");
            login_card.login = true;
        }
        callback()
    });
}

// 复选框样式
const checkbox_CSS = ref("login_card--checkbox_f");
function changeCheckbox() {
    user.remember = !user.remember;
    if (user.remember) {
        checkbox_CSS.value = "login_card--checkbox_t";
    } else {
        checkbox_CSS.value = "login_card--checkbox_f";
    }
}
</script>

<style lang="scss" scoped>
// 复选框
// .login_card--checkbox_t {
//     background-color: aqua;
// }
.login_card--checkbox_uninstall {
    display: none;
}

// 复选款动画
// 转到注册动画
@keyframes login_card--checkbox_f {
    90% {
        transform: scale(1, 1.1);
        transform: scale(1, 1) rotate(2deg);
    }
}
@keyframes login_card--checkbox_t {
    90% {
        transform: scale(1, 1.1);
        transform: scale(1, 1) rotate(2deg);
    }
}
.login_card--checkbox_f {
    animation-name: login_card--checkbox_f;
    animation-duration: 0.1s;
}
.login_card--checkbox_t {
    animation-name: login_card--checkbox_t;
    animation-duration: 0.1s;
    background-color: var(--main-color-b);
}
// 转到注册动画
@keyframes login_card--input_register_t {
    0% {
        transform: scale(1, 0);
        height: 0px;
        margin: 0px;
    }
    100% {
    }
}
@keyframes login_card--input_register_f {
    0% {
    }
    100% {
        transform: scale(1, 0);
        height: 0px;
        margin: 0px;
    }
}

// 转到注册分割线动画
.login_card--input_register_t {
    animation-name: login_card--input_register_t;
    animation-duration: 0.2s;
}
.login_card--input_register_f {
    animation-name: login_card--input_register_f;
    animation-duration: 0.2s;
    animation-fill-mode: both;
    // display: none;
}
@keyframes login_card--input_register_hr_t {
    0% {
        transform: scale(1, 1);
    }
    0% {
        transform: scale(1, 0);
    }
}
.login_card--input_register_hr_f {
    display: none;
}
.login_card--input_register_hr_t {
    animation-name: login_card--input_register_hr_t;
    animation-duration: 0.2s;
    animation-fill-mode: both;
}

// 按钮动画
@keyframes login_card--button_t {
    0% {
        flex: 1;
    }
    40% {
        flex: 2.5;
        color: var(--main-color-a);
        font-size: 60px;
    }
    100% {
        transform: scale(1.1, 1.1);
        flex: 2.5;
        color: var(--main-color-a);
        font-size: 20px;
        font-weight: 900;
    }
}
.login_card--button {
    div:hover {
        // background-color: aqua;
        animation-name: login_card--button_t;
        animation-duration: 0.2s;
        animation-fill-mode: both;
    }
}

// 输入框动画
@keyframes login_card--input_t {
    0% {
        transform: scale(0, 1);
    }
    100% {
        transform: scale(1, 1);
    }
}
.login_card--input {
    animation-name: login_card--input_t;
    animation-duration: 0.2s;
}
// 输入框载入动画
@keyframes login_card--input_hover--t {
    0% {
        transform: scale(1, 1);
    }
    100% {
        transform: scale(1, 1.6);
    }
}
@keyframes login_card--input_focus--t {
    0% {
    }
    100% {
        transform: translate(10px, 0);
    }
}
.login_card--input {
    div {
        input:focus + hr {
            animation-name: login_card--input_hover--t;
            animation-duration: 0.2s;
        }
        input:focus {
            animation-name: login_card--input_focus--t;
            animation-duration: 0.2s;
            animation-fill-mode: forwards;
        }
    }
}

// 输入框整体样式
.login_card {
    transform: translate(0, 0);

    max-width: 500px;
    min-width: 500px;
    background-color: #3e3e3e1d;
    backdrop-filter: blur(7px);
    border-radius: 4px;

    .login_card--title {
        cursor: default;
        user-select: none;

        height: 70px;
        line-height: 70px;
        font-size: 70px;
        font-weight: 1000;
        text-align: center;
        color: #000000;

        text-shadow: 2px 2px 5px rgba(92, 92, 92, 0.8);

        div {
            position: fixed;
            transform: translate(0px, 0px) scale(1.4, 1.4);
            span {
                color: var(--main-color-a);
            }
        }
    }
    .login_card--input {
        margin: 20px 0 40px 0;

        div {
            width: 80%;
            margin: 0 auto;
            input {
                margin: 0 auto;
                height: 40px;
                padding: 0;
                background: rgba(67, 54, 54, 0);
                border: 0px;
                font-size: 20px;
            }
            input:focus {
                outline: none;
            }
            input:-moz-placeholder,
            textarea:-moz-placeholder {
                color: #5f5f5f;
            }
            input:-ms-input-placeholder,
            textarea:-ms-input-placeholder {
                color: #5f5f5f;
            }
            input::-webkit-input-placeholder,
            textarea::-webkit-input-placeholder {
                color: #5f5f5f;
            }

            hr {
                height: 3px;
                border: none;
                border-top: 6px double var(--main-color-a);
            }
        }
    }
    .login_card--checkbox {
        position: fixed;
        top: 230px;
        div {
            margin-left: 478px - 50px;
            width: 16px;
            height: 16px;
            border: 2px solid#464646;
            user-select: none;
            span {
                display: block;
                margin-left: -120px;
                color: #5f5f5f;
            }
        }
    }
    .login_card--holder {
        padding-bottom: 50px;
    }
    .login_card--button {
        transform: translate(0, -50px);
        position: absolute;
        width: 100%;
        height: 50px;
        display: flex;
        justify-content: space-around;

        .login_card--button_a,
        .login_card--button_b,
        .login_card--button_c {
            white-space: nowrap;
            margin: 0px 0px;
            flex: 1;
            width: 40%;
            text-align: center;
            line-height: 50px;
            font-size: 50px;
            font-weight: 100;
            color: #46464660;
            border-radius: 2px;
        }

        .login_card--button_a {
            cursor: pointer;
            user-select: none;
        }
        .login_card--button_b {
            cursor: pointer;
            user-select: none;
        }
        .login_card--button_c {
            cursor: pointer;
            user-select: none;
        }
    }
}
</style>
